window.onload = function () {
    getAllDept();
    getMajorByDeptId();
    getClazzByMajorId();
}

var table;
layui.use(['layer', 'laydate', 'table'], function () {
    $ = layui.jquery;
    var layer = layui.layer,
        laydate = layui.laydate;
    table = layui.table;

    laydate.render({
        elem: '#recordDate',
        value: new Date()
    });

    // 学生
    // 初始化表格
    table.render({
        //设置ID
        id: 'studentTable',
        // 指定原始 table 容器
        elem: '#studentTableId',
        // 异步数据接口
        url: '/student/getStudentInfo',
        where: {stuId: '', stuName: '', clazzId: ''},
        // 设置导出按钮
        toolbar: '#toolBar',
        //设置表头。值是一个二维数组
        cols: [
            [{
                type: 'numbers',
                width: 50,
                title: '序号',
                fixed: "left"
            }, {
                field: 'stuId',
                width: 100,
                title: '学号',
                align: "center"
            }, {
                field: 'stuName',
                width: 100,
                title: '姓名',
                align: "center"
            }, {
                field: 'sex',
                width: 50,
                title: '性别',
                align: "center"
            }, {
                field: 'phoneNumber',
                width: 110,
                title: '联系方式',
                align: "center"
            }, {
                field: 'kinsfolkName',
                width: 120,
                title: '父母姓名',
                align: "center"
            }, {
                field: 'kinsfolkPhoneNumber',
                width: 110,
                title: '父母联系方式',
                align: "center"
            }, {
                field: 'clazzName',
                minWidth: 130,
                title: '班级',
                align: "center"
            }, {
                field: 'majorName',
                minWidth: 130,
                title: '专业',
                align: "center"
            }, {
                field: 'deptName',
                minWidth: 130,
                title: '院系',
                align: "center"
            }, {
                //     field: 'grade',
                //     width: 100,
                //     title: '年级',
                //     align: "center"
                // }, {
                title: '操作',
                minWidth: 150,
                templet: '#studentTableBar',
                fixed: "right",
                align: "center"
            }]
        ],
        // 每页条数的选择项
        limits: [5, 10, 15, 20, 25, 50, 100],
        // 每页显示的条数
        limit: 15,
        // 是否开启分页
        page: true
    });

    table.on('toolbar(studentTableFilter)', function (obj) {
        var param = obj.data;
        if (obj.event === 'add') {
            var othis = $(this),
                method = othis.data('method');
            active[method] ? active[method].call(this, othis, param) : '';
        }
    });

    // 监听表格编辑、删除按钮
    table.on('tool(studentTableFilter)', function (obj) {
        var param = obj.data;
        console.log(param);
        if (obj.event === 'modify') {
            var othis = $(this),
                method = othis.data('method');
            active[method] ? active[method].call(this, othis, param) : '';
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除该学生信息吗?', function (index) {
                removeStu(param);
                layer.close(index);
            });
        }
    });

    // 定义触发事件弹出厨框
    var active = {
        addStuInfo: function (othis, param) {
            layer.open({
                id: 'addstudent_iframe', // 防止重复弹出
                title: '学生信息',
                type: 2,
                offset: ['5%', '28%'],
                area: ['40%', '80%'],
                content: ['addStudent'],
                skin: 'layui-layer-molv',
                // btn: '关闭全部',
                // btnAlign: 'c' ,//按钮居中
                // yes: function() {
                // 	layer.closeAll();
                // }
                shade: 0.3, //不显示遮罩
                maxmin: true, //开启最大化按钮
                anim: 3, // 过渡效果（3或者4）
                resize: false, // 禁止窗体拉伸
                // time : 1000,//定时关闭
                success: function (layero, index) { //弹出层弹出后的回调函数
                    console.log(layero, index);
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    //console.log(body.html()) //得到iframe页的body内容
                    body.find('#teamid').val(param.teamid);

                    // 渲染页面表单，否则不显示数据
                    iframeWin.layui.form.render();
                }
            });
        },
        modifyStuInfo: function (othis, param) {
            layer.open({
                id: 'modifyuser_iframe', // 防止重复弹出
                title: '学生信息',
                type: 2,
                offset: ['5%', '28%'],
                area: ['40%', '80%'],
                content: ['modifyStuInfo'],
                skin: 'layui-layer-molv',
                // btn: '关闭全部',
                // btnAlign: 'c' ,//按钮居中
                // yes: function() {
                // 	layer.closeAll();
                // }
                shade: 0.3, //不显示遮罩
                maxmin: true, //开启最大化按钮
                anim: 3, // 过渡效果（3或者4）
                resize: false, // 禁止窗体拉伸
                // time : 1000,//定时关闭
                success: function (layero, index) { //弹出层弹出后的回调函数
                    console.log(layero, index);
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    //console.log(body.html()) //得到iframe页的body内容
                   body.find('#stuId').val(param.stuId);
                   body.find('#stuName').val(param.stuName);
                   body.find('#sex').val(param.sex);
                   body.find('#birthplace').val(param.birthplace);
                   body.find('#birthday').val(param.birthday);
                   body.find('#phoneNumber').val(param.phoneNumber);
                   body.find('#kinsfolkName').val(param.kinsfolkName);
                   body.find('#kinsfolkPhoneNumber').val(param.kinsfolkPhoneNumber);
                   body.find('#grade').val(param.grade);
                   body.find('#remark').val(param.remark);


                    // 渲染页面表单，否则不显示数据
                    iframeWin.layui.form.render();
                }
            });
        }
    };
});

function searchStuInfo() {
    //执行搜索重载
    table.reload('studentTable', {
        page: {
            curr: 1
        },
        where: {
            stuId: $("#stuId").val(),
            stuName: $("#stuName").val(),
            clazzId: $("#clazz").val()
        }
    }, 'data');
}

function getAllDept() {
    $.ajax({
        type: "POST",
        url: "/common/getAllDept",
        contentType: "application/json",
        sync: true,
        data: {},
        dataType: "json",
        success: function (result) {
            $("#department").empty();
            $("#department").append("<option value=''>请选择院系</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '<option value=' + o.deptId + '>' + o.deptName + '</option>';
                    $("#department").append(opt);
                });
            }
            $("#department").selectpicker('refresh');//动态加载
        }
    });
}

function getMajorByDeptId() {
    // 获取表单参数
    var deptId = $("#department option:selected").val();
    var param = {
        deptId: deptId
    }
    // 校验成功，执行添加请求
    $.ajax({
        type: "POST",
        url: "/common/getMajorByDeptId",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#major").empty();
            $("#major").append("<option value=''>请选择专业</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '<option value=' + o.majorId + '>' + o.majorName + '</option>';
                    $("#major").append(opt);
                });
            }
            $("#major").selectpicker('refresh');//动态加载
        }
    });
}

function getClazzByMajorId() {
    // 获取表单参数
    var majorId = $("#major").val();
    var param = {
        majorId: majorId
    }
    // 校验成功，执行添加请求
    $.ajax({
        type: "POST",
        url: "/common/getClazzByMajorId",
        // contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            $("#clazz").empty();
            $("#clazz").append("<option value=''>请选择班级</option>");
            if (result.code == 200) {
                $.each(result.data, function (i, o) {
                    var opt = '<option value=' + o.clazzId + '>' + o.clazzName + '</option>';
                    $("#clazz").append(opt);
                });

            }
            $("#clazz").selectpicker('refresh');//动态加载
        }
    });
}

function removeStu(data) {
    var param = {
        stuId: data.stuId
    }
    $.ajax({
        type: "POST",
        url: "/student/removeStuByStuId",
        //contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            if (result.code == 200) {
                searchStuInfo();
                layer.msg('删除成功！', {icon: 1, offset: '100px', time: 2000});
            } else {
                layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
            }
        }
    });
}